//package com.zzyl.nursing.service.impl;
//
//import java.util.List;
//import com.zzyl.common.utils.DateUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import com.zzyl.nursing.mapper.NursingPlanMapper;
//import com.zzyl.nursing.domain.NursingPlan;
//import com.zzyl.nursing.service.INursingPlanService;
//
///**
// * 护理计划Service业务层处理
// *
// * @author ruoyi
// * @date 2025-10-07
// */
//@Service
//public class NursingPlanServiceImpl implements INursingPlanService
//{
//    @Autowired
//    private NursingPlanMapper nursingPlanMapper;
//
//    /**
//     * 查询护理计划
//     *
//     * @param id 护理计划主键
//     * @return 护理计划
//     */
//    @Override
//    public NursingPlan selectNursingPlanById(Long id)
//    {
//        return nursingPlanMapper.selectNursingPlanById(id);
//    }
//
//    /**
//     * 查询护理计划列表
//     *
//     * @param nursingPlan 护理计划
//     * @return 护理计划
//     */
//    @Override
//    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
//    {
//        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
//    }
//
//    /**
//     * 新增护理计划
//     *
//     * @param nursingPlan 护理计划
//     * @return 结果
//     */
//    @Override
//    public int insertNursingPlan(NursingPlan nursingPlan)
//    {
//        nursingPlan.setCreateTime(DateUtils.getNowDate());
//        return nursingPlanMapper.insertNursingPlan(nursingPlan);
//    }
//
//    /**
//     * 修改护理计划
//     *
//     * @param nursingPlan 护理计划
//     * @return 结果
//     */
//    @Override
//    public int updateNursingPlan(NursingPlan nursingPlan)
//    {
//        nursingPlan.setUpdateTime(DateUtils.getNowDate());
//        return nursingPlanMapper.updateNursingPlan(nursingPlan);
//    }
//
//    /**
//     * 批量删除护理计划
//     *
//     * @param ids 需要删除的护理计划主键
//     * @return 结果
//     */
//    @Override
//    public int deleteNursingPlanByIds(Long[] ids)
//    {
//        return nursingPlanMapper.deleteNursingPlanByIds(ids);
//    }
//
//    /**
//     * 删除护理计划信息
//     *
//     * @param id 护理计划主键
//     * @return 结果
//     */
//    @Override
//    public int deleteNursingPlanById(Long id)
//    {
//        return nursingPlanMapper.deleteNursingPlanById(id);
//    }
//}
package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-10
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //查询护理计划
        NursingPlan nursingPlan = getById(id);

        //查询护理计划和护理项目的关联表
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.selectByPlanId(id);

        //封装到 vo 返回
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
//     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    @Transactional //事务
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //将dto转换位实体对象
        NursingPlan nursingPlan = new NursingPlan();
//        nursingPlan.setPlanName(nursingPlanDto.getPlanName());
//        nursingPlan.setSortNo(nursingPlanDto.getSortNo());
//        nursingPlan.setStatus(nursingPlanDto.getStatus());
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        //保存护理计划
        //这个save哪来的?: 继承了ServiceImpl
        boolean saveResult = save(nursingPlan);
        //批量保存护理计划项目
        List<NursingProjectPlan> projectPlan = nursingPlanDto.getProjectPlans();
        if(projectPlan != null && !projectPlan.isEmpty()){
            projectPlan.forEach(item -> item.setPlanId(nursingPlan.getId()));
            nursingProjectPlanMapper.insertBatch(projectPlan);
        }
        return saveResult ? 1 : 0 ;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.更新护理计划
        //拷贝dto到实体对象
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        boolean updateResult =  updateById(nursingPlan);
        //2.根据护理计划的id删除中间表
        LambdaQueryWrapper<NursingProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingProjectPlan::getPlanId, nursingPlan.getId());
        nursingProjectPlanMapper.delete(queryWrapper);
        //3.批量保存
        List<NursingProjectPlan> projectPlan = nursingPlanDto.getProjectPlans();
        if(projectPlan != null && !projectPlan.isEmpty()){
            projectPlan.forEach(item -> item.setPlanId(nursingPlan.getId()));
            nursingProjectPlanMapper.insertBatch(projectPlan);
        }
        return updateResult ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return removeById(id)? 1 : 0;
    }
}
